package com.platform.qujia.pay.strategy.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.platform.qujia.config.BasicInfo;
import com.platform.qujia.constantEnum.TianQuetechPayEnum;
import com.platform.qujia.param.request.TradeReverseScanPayParameter;
import com.platform.qujia.pay.QueryOrderStrategy;
import com.platform.qujia.pojo.PayWay;
import com.platform.qujia.pojo.TradePaymentRecordsDO;
import com.platform.qujia.utils.ApiRequestBean;
import com.platform.qujia.utils.RSASignature;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @date 2023/8/11
 */
@Component
public class SXFQueryOrder implements QueryOrderStrategy {
    
    private static final Logger log = LoggerFactory.getLogger(SXFQueryOrder.class);

    @Autowired
    private RestTemplate restTemplate;
    
    @Override
    public TianQuetechPayEnum getOrderStatus(TradeReverseScanPayParameter param,TradePaymentRecordsDO record, PayWay payWay) {

        param.setMno(payWay.getMno());
        param.setOrgId(BasicInfo.TRADE_REVERSE_PAY_ORGID);

        ApiRequestBean<JSONObject> reqBean = new ApiRequestBean<JSONObject>();
        log.info("随行支付-订单查询"+param.getOrdNo());
        reqBean.setSignType("RSA");
        reqBean.setVersion("1.0");
        JSONObject reqData = (JSONObject) JSONObject.toJSON(param);
        reqBean.setReqData(reqData);
        reqBean.setOrgId(param.getOrgId());
        reqBean.setReqId(param.getReqId());
        reqBean.setTimestamp(param.getTimestamp());
        //业务参数
        reqData.put("mno", param.getMno()); //商户编号
        reqData.put("ordNo", record.getOrderSn()); //商户订单号
        reqBean.setReqData(reqData);
        String req = JSONObject.toJSONString(reqBean);
        //此处不要改变reqData里面值的顺序用LinkedHashMap
        @SuppressWarnings("unchecked")
        HashMap<String, Object> reqMap = JSON.parseObject(req, LinkedHashMap.class, Feature.OrderedField);
        //组装加密串
        String signContent = RSASignature.getOrderContent(reqMap);
        try {
            String sign = RSASignature.encryptBASE64(RSASignature.sign(signContent, BasicInfo.TRADE_REVERSE_PAY_PRIVATEKEY));
            reqMap.put("sign", sign);
            String reqStr = JSON.toJSONString(reqMap);
            log.info("随行付订单查询参数：{}",reqStr);
            //String resultJson = HttpUtils.connectPostUrl(BasicInfo.TRADE_REVERSE_PAY_TIANQUEURL+BasicInfo.TRADE_REVERSE_PAY_TRADEQUERYURL, reqStr);
            ResponseEntity<String> res = restTemplate.postForEntity(BasicInfo.TRADE_REVERSE_PAY_TIANQUEURL+BasicInfo.TRADE_REVERSE_PAY_TRADEQUERYURL, reqMap, String.class);
            String resultJson = res.getBody();
            log.info("随行付订单查询返回结果：{}",resultJson);
            JSONObject result = JSONObject.parseObject(resultJson);
            if ("0000".equals(result.get("code"))) {
                JSONObject respData = result.getJSONObject("respData");
                String bizCode = respData.getString("bizCode");
                String tradeCode = respData.getString("tradeCode");
                String tranSts = respData.getString("tranSts");
                if (StringUtils.equals("0000",bizCode)) {
                    if (com.platform.qujia.utils.StringUtils.isNotEmpty(tradeCode)) {
                        if(StringUtils.equals("0000",tradeCode)) {
                            return TianQuetechPayEnum.PAY_SUCCESS;
                        }
                        if(StringUtils.equals("2002",tradeCode)) {
                            return TianQuetechPayEnum.PAY_PAYING;
                        }
                        if(StringUtils.equals("4002",tradeCode)) {
                            return TianQuetechPayEnum.PAY_FAIL;
                        }
                    }else{
                        if(StringUtils.equals("SUCCESS",tranSts)) {
                            return TianQuetechPayEnum.PAY_SUCCESS;
                        }
                        if(StringUtils.equals("PAYING",tranSts)) {
                            return TianQuetechPayEnum.PAY_PAYING;
                        }
                        return TianQuetechPayEnum.PAY_FAIL;
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
}
